﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Component;
using DevExpress.Xpo;
public partial class FrontEnd_Body_Center_Contact : System.Web.UI.UserControl
{

    ArrayList al = new ArrayList();
    Cart _cart = null;
    public string _MaillError = "";
      public string _PhoneError = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        this.rfvCty.ErrorMessage = "*";
        this.rfvDienThoai.ErrorMessage = "*";
        this.rfvHoTen.ErrorMessage = "*";
        this.rfvMail.ErrorMessage = "*";
        this.rfvQuocGia.ErrorMessage = "*";
        this.rfvWard.ErrorMessage = "*";
        this.ddlProvince.DataTextField = ColumProvince._Name;
        this.ddlProvince.DataValueField = ColumProvince._Code;
        this.ddlProvince.SelectedIndex = ProvincesDB.GetAll().Count - 1;
        this.ddlProvince.DataSource = ProvincesDB.GetAll();
        this.ddlProvince.DataBind();
        this.btnHuy.Text = Component.Languages.SearchByKeyWord("Huy");
        this.btnSend.Text = Component.Languages.SearchByKeyWord("Goi");
    }

    protected void GetWard(object sender, EventArgs e)
    {
        string code = this.ddlProvince.SelectedItem.Value;
        this.ddlWard.DataTextField = ColumWard._Name;
        this.ddlWard.DataValueField = ColumWard._Code2;
        this.ddlWard.DataSource = WardsDB.GetAllObjCode1Province(code);
        this.ddlWard.DataBind();
    }
    protected void Send(object sender, EventArgs e)
    {
        if (QueryHelper.GetQueryString(Request, ConstString._LienHeGia) == ConstString._LienHeGia)
        {
            _cart = new Cart(KetNoiCSDL.session);
            _cart.CreatDate = DateTime.Now.Date;
            CartDB.Save(_cart);
            SaveCartProduct();
        }
        Contact contact = new Contact(KetNoiCSDL.session);
        contact.Address = this.txtDiaChi.Text;
        contact.CompanyName = this.txtCty.Text;
        contact.Content = this.txtNoiDung.Text;
        contact.Country = this.txtQuocGia.Text;
        contact.Date = DateTime.Now.Date;
        contact.FullName = this.txtHoTen.Text;
        if (Config.CheckFormatEmail(this.txtMail.Text) == false)
        {
            this.txtMail.Focus();
           // this.txtMail.BackColor = System.Drawing.Color.;
           _MaillError =Languages.SearchByKeyWord("vdu")+" : binhtn@butxanh.com";
            return;
        }
        if (Config.CheckDigitNumber(this.txtDienThoai.Text) == false)
        {
            this.txtDienThoai.Focus();
           // this.txtDienThoai.BackColor = System.Drawing.Color.;
            _PhoneError = Languages.SearchByKeyWord("ktso");
            return;
        }
        contact.Mail = this.txtMail.Text;
        contact.Phone = this.txtDienThoai.Text;
        contact.Province = this.ddlProvince.SelectedItem.Text;
        contact.Ward = this.ddlWard.SelectedItem.Text;
        contact.Cart = _cart != null ? _cart.Id : 0;
        contact.Sy = this.rblHinhThucMua.SelectedItem.Text;
        if (ContactDB.Save(contact) == 1)
        {

            if (Sessions.IdProducts != "")
            {
                Sessions.IdProducts = null;
                Response.Redirect(ConstString._Default + ConstString._ServiceTransport);

            }
            else
            {
                Response.Redirect(ConstString._Default + ConstString._MessageContact);
            }
        }
    }
    protected void Huy(object sender, EventArgs e)
    {
        Sessions.IdProducts = null;
        this.txtDiaChi.Text = "";
        this.txtCty.Text = "";
        this.txtNoiDung.Text = "";
        this.txtQuocGia.Text = "";
        this.txtHoTen.Text = "";
        this.txtMail.Text = "";
        this.txtDienThoai.Text = "";
    }
    void SaveCartProduct()
    {
        Cart_Product cartProduct = null;
        DataList dtlist = CartDetail1.FindControl("dlCartDetail") as DataList;
        if (dtlist.Items.Count > 0)
        {

            for (int i = 0; i < dtlist.Items.Count; i++)
            {
                cartProduct = new Cart_Product(KetNoiCSDL.session);
                TextBox txtQuantity = (dtlist.Items[i].FindControl("txtNumber")) as TextBox;
                cartProduct.Quantity = ConvertType.ToInt(txtQuantity.Text);
                cartProduct.Cart = _cart;
                HiddenField idProduct = (dtlist.Items[i].FindControl("idProduct")) as HiddenField;
                cartProduct.Product = ProductDB.GetObjectByid(ConvertType.ToInt(idProduct.Value), false);
                Cart_ProductDB.Save(cartProduct);
            }
        }
        else
            CartDB.Delete(_cart);
    }
  
   
}
